<script setup>
import {ref,onMounted} from 'vue'
import axios from "axios";
import {ElMessage,ElMessageBox} from "element-plus";

onMounted(()=>{
  getAllCouponList()
})
// 优惠券列表
const tableData = ref([])
const req = ref({
  pageNum:1,
  pageSize:3
})
const total = ref()
const getAllCouponList = () => {
  axios.post('api/product/selAllProductList',req.value).then(
      res=>{
        tableData.value = res.data.data.list
        total.value = res.data.data.total
      }
  )
}
const handleSizeChange = () => {
  req.value.pageNum = 1
  getAllCouponList()
}
const handleCurrentChange = () => {
  getAllCouponList()
}

//购买商品
const dialogFormVisible = ref(false)
const bought = ref({})


//回显商品信息
const couponList = ref([])
const handleEdit = (row) => {
  // 显示购买弹框
  dialogFormVisible.value = true

  // 回显商品信息
  bought.value = {
    productName: row.productName,
    productType: row.dictProductType,
    price: row.price,
    boughtTime: new Date().toISOString().slice(0, 10), // 当前日期
    num: 1,
    allMoney: row.price,
    couponId: '',
    discountAmount: 0,
    afterMoney: row.price
  }

  let productId = row.id
  axios.get('api/coupon/selCouponList/'+productId).then(
      res=>{
        couponList.value = res.data.data
      }
  )
}
//购买
const boughtProduct = () => {
  axios.post('api/product/boughtProducts',bought.value).then(
      res=>{
        if(res.data.code === 200){
          ElMessage.success("购买成功")
        }else {
          ElMessage.error(res.data.msg)
        }
      }
  )
}
</script>

<template>
  <!--  列表-->
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="id" label="序号" width="180" />
    <el-table-column prop="productName" label="产品名称" width="180" />
    <el-table-column prop="dictProductType" label="产品分类" width="180" />
    <el-table-column prop="inventory" label="库存" width="180">
      <template #default="scope">
        <span :class="{ 'low-inventory': scope.row.inventory < 10 }">
          {{ scope.row.inventory }}
        </span>
      </template>
    </el-table-column>
    <el-table-column prop="price" label="价格" width="180" />
    <el-table-column label="Operations">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)">
          购买
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <!--  分页-->
  <el-pagination
      v-model:current-page="req.pageNum"
      v-model:page-size="req.pageSize"
      :page-sizes="[3, 5, 10, 12]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
  />

  <!--  弹框-->
  <el-dialog v-model="dialogFormVisible" title="购买商品" width="500">
    <el-form :model="bought">
      <el-form-item label="购买时间">
        <el-input v-model="bought.boughtTime" />
      </el-form-item>
      <el-form-item label="产品名称">
        <el-input v-model="bought.productName" readonly class="readonly-input" />
      </el-form-item>
      <el-form-item label="产品分类">
        <el-input v-model="bought.productType" readonly class="readonly-input" />
      </el-form-item>
      <el-form-item label="单价">
        <el-input v-model="bought.price" readonly class="readonly-input" />
      </el-form-item>
      <el-form-item label="数量">
        <el-input v-model="bought.num" />
      </el-form-item>
      <el-form-item label="总价">
        <el-input v-model="bought.allMoney" />
      </el-form-item>
      <el-form-item label="优惠券">
        <el-select v-model="bought.couponId" placeholder="Select" style="width: 240px">
          <el-option
              v-for="item in couponList"
              :key="item.id"
              :label="item.couponName"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优惠金额">
        <el-input v-model="bought.discountAmount" />
      </el-form-item>
      <el-form-item label="优惠后金额">
        <el-input v-model="bought.afterMoney" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false;bought = {}">取消</el-button>
        <el-button type="primary" @click="boughtProduct">
          购买
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.low-inventory {
  color: red;
  font-weight: bold;
}

.readonly-input {
  background-color: #f5f7fa;
  color: #909399;
}

.readonly-input :deep(.el-input__inner) {
  background-color: #f5f7fa;
  color: #909399;
  cursor: not-allowed;
}
</style>
